package com.gxkj.projects.myshopx.services.impl;

import com.gxkj.common.exceptions.BusinessException;
import com.gxkj.common.exceptions.ValidateException;
import com.gxkj.common.utils.ListPager;
import com.gxkj.common.utils.ValidatorUtil;
import com.gxkj.projects.myshopx.dao.AttributeDaoImpl;
import com.gxkj.projects.myshopx.entitys.Attribute;
import com.gxkj.projects.myshopx.entitys.User;
import com.gxkj.projects.myshopx.enums.AttributeInputTypeEnum;
import com.gxkj.projects.myshopx.enums.AttributeStates;
import com.gxkj.projects.myshopx.enums.ErrorCodeEnum;
import com.gxkj.projects.myshopx.services.AttributeService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AttributeServiceImpl implements AttributeService {

    Logger LOG = LoggerFactory.getLogger(AttributeServiceImpl.class);

    @Autowired
    private ValidatorUtil validatorUtil;

    @Autowired
    private AttributeDaoImpl attributeDao;

    public void doAddAttribute(User user, Attribute entity) throws ValidateException {

        entity.setCreatedAt(new Date());
        entity.setUpdatedAt(new Date());
        entity.setStates(AttributeStates.NORMAL);
        if(StringUtils.isBlank(entity.getAttrGroup())){
            entity.setAttrGroup("");
        }
        if(entity.getAttrInputType() != AttributeInputTypeEnum.SELECT){
            entity.setAttrValues("");
        }
        validatorUtil.validate(entity,true);
        if(attributeDao.isExitByName(null,entity.getAttrName(),entity.getTypeId())){
            BusinessException businessException = new BusinessException(ErrorCodeEnum.BUSSINESS_ERROR.getCode(),"该名称已经存在");
            throw businessException;
        }

        attributeDao.insert(entity);
    }


    public void doUpdateAttribute(User user, Attribute entity) throws ValidateException {

        Attribute dbentity = this.getById(entity.getId());
        entity.setStates(dbentity.getStates());
        entity.setCreatedAt(dbentity.getCreatedAt());
        entity.setUpdatedAt(new Date());

        if(StringUtils.isBlank(entity.getAttrGroup())){
            entity.setAttrGroup("");
        }
        if(entity.getAttrInputType() != AttributeInputTypeEnum.SELECT){
            entity.setAttrValues("");
        }
        validatorUtil.validate(entity,true);
        if(attributeDao.isExitByName(entity.getId(),entity.getAttrName(),entity.getTypeId())){
            BusinessException businessException = new BusinessException(ErrorCodeEnum.BUSSINESS_ERROR.getCode(),"该类型下名称已经存在");
            throw businessException;
        }
        attributeDao.updateByMerge(entity);

    }


    public void doDeleteAttribute(User user, String id) {
        Attribute dbentity = this.getById(id);
        dbentity.setStates(AttributeStates.DEL);
        attributeDao.updateByMerge(dbentity);
    }


    public ListPager<Attribute> doPageHQL(int pagenNo, int pageSize, Attribute entity)  {


        return attributeDao.doPageHQL(pagenNo,pageSize,entity);
    }


    public Attribute getById(String id) {
        Attribute dbentity = attributeDao.selectById(id,Attribute.class);
        return dbentity;
    }


    public List<Attribute> getAttributeListByTypeId(String goodsTypeId) {
        return attributeDao.getAttributeListByTypeId( goodsTypeId);
    }
}
